package com.sinping.qh.api.base;

import com.sinping.qh.api.exception.HandlerException;
import com.sinping.qh.api.support.ConstEum;
import com.sinping.qh.dto.mongo.auth.UserSessionDTO;
import com.sinping.qh.service.app.login.ILoginService;
import com.sinping.qh.service.redis.RedisService;
import com.sinping.qh.utils.mapper.JsonMapper;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Set;

/**
 * 是否登录检查放入到filter中
 * @author zhangjin
 * @create 2018/5/31
 */
@Data
public class BaseEndPoint {


    protected final static Logger logger = LoggerFactory.getLogger(BaseEndPoint.class);

    @Autowired
    private HttpServletRequest request;


    @Autowired
    ILoginService iLoginService;

    @Autowired
    private Validator globalValidator;

    @Autowired
    RedisService redisService;


    public static final String token_name = "token";

    public static final String mPlatform = "platform";

    /***************************************************************
     ************************获取数据区********************************
     ****************************************************************/

    /**
     * 获取token
     * @return
     */
    public String getToken()
    {
        String token = request.getHeader(token_name);
        return token;
    }

    /**
     * 获取请求平台
     * @return
     */
    public String getRequestPlatform()
    {
        String platform = request.getHeader(mPlatform);
        return platform;
    }


    public UserSessionDTO getUserSessionDto()
    {
        String userSessionDTO=
                redisService.hgetByUserinfo(getToken());
        UserSessionDTO newUserSessionDTO=
                JsonMapper.defaultMapper().fromJson(userSessionDTO,UserSessionDTO.class);
        return newUserSessionDTO;
    }


    /***************************************************************
     ************************错误检查区********************************
     ****************************************************************/

    /**
     * 检查参数是否为null,显示默认的消息内容
     * @param
     */
    protected <T> void  checkNull(T t,ConstEum constEum)
    {
        checkNull(t,constEum,"");
    }

    /**
     * 检查参数是否为null,自定义消息内容
     * @param
     */
    protected <T> void  checkNull(T t,ConstEum constEum,String message)
    {
        String error=message;
        if(StringUtils.isEmpty(error))
        {
            error=constEum.getMessage();
        }
        if(t instanceof String)
        {
            if(!StringUtils.isNotBlank(String.valueOf(t)))
            {
                logger.error(error);
                throw new HandlerException(constEum,error);
            }

        }else if (t == null) {
            logger.error(error);
            throw new HandlerException(constEum,error);
        }
    }

    /**
     * 验证所有错误
     * @param t
     * @param oneShow 为true 表示一个一个错误显示
     * @param <T>
     */
    protected <T> void checkValidator(T t,Class group,boolean oneShow)
    {
        //验证注解
        Set<ConstraintViolation<T>> validate = globalValidator.validate(t, group);
        if (!validate.isEmpty()) {
            StringBuffer buffer = new StringBuffer(64);
            for (ConstraintViolation<T> apiOrgSalesDTOConstraintViolation : validate) {
                buffer.append(apiOrgSalesDTOConstraintViolation.getMessage());
                if(oneShow)
                {
                    break;
                }
            }
            throw new HandlerException(ConstEum.FORMAT_ERROR, buffer.toString());
        }


    }







}
